<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" draggable width="100%">
		<el-tabs v-model="activeName" class="demo-tabs">
			<el-tab-pane label="基本信息" name="first">
				<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" style="height:450px;" label-width="120px" @keyup.enter="submitHandle()">
					<div class="divline">
						<el-form-item prop="username" class="divlineleft" label="用户名">
							<el-input v-model="dataForm.username" placeholder="用户名"></el-input>
						</el-form-item>
						<el-form-item prop="realName" class="divlineleft" label="姓名">
							<el-input v-model="dataForm.realName" placeholder="姓名"></el-input>
						</el-form-item>
					</div>
					<div class="divline">
						<el-form-item prop="deptName" class="divlineleft" label="部门">
							<el-input v-model="dataForm.deptName" placeholder="部门" @click="addOrUpdateDeptHandle()"></el-input>
						</el-form-item>
						<el-form-item prop="gender" class="divlineleft" label="性别">
							<fast-radio-group v-model="dataForm.gender" dict-type="user_gender"></fast-radio-group>
						</el-form-item>
					</div>
					<div class="divline">
						<el-form-item prop="mobile" class="divlineleft" label="手机号">
							<el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
						</el-form-item>
						<el-form-item prop="email" class="divlineleft" label="邮箱">
							<el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
						</el-form-item>
					</div>
					<div class="divline">
						<el-form-item prop="password" class="divlineleft" label="密码">
							<el-input v-model="dataForm.password" type="password" placeholder="密码"></el-input>
						</el-form-item>
						<el-form-item prop="roleIdList" class="divlineleft" label="所属角色">
							<el-select v-model="dataForm.roleIdList" multiple placeholder="所属角色" style="width: 100%">
								<el-option v-for="role in roleList" :key="role.id" :label="role.name" :value="role.id"></el-option>
							</el-select>
						</el-form-item>
					</div>
					<div class="divline">
						<el-form-item prop="postIdList" class="divlineleft" label="所属岗位">
							<el-select v-model="dataForm.postIdList" multiple placeholder="所属岗位" style="width: 100%">
								<el-option v-for="post in postList" :key="post.id" :label="post.postName" :value="post.id"></el-option>
							</el-select>
						</el-form-item>
						<el-form-item prop="status" class="divlineleft" label="状态">
							<fast-radio-group v-model="dataForm.status" dict-type="user_status"></fast-radio-group>
						</el-form-item>
					</div>
					<div class="divline">
						<el-form-item prop="userSex" class="divlineleft" label="性别">
							<el-select v-model="dataForm.userSex" style="width: 100%">
									<el-option
									v-for="item in dataForm.sexlist"
									:key="item.value"
									:label="item.label"
									:value="item.value"
									/>
								</el-select>
						</el-form-item>
						<el-form-item prop="userCard" class="divlineleft" label="身份证号">
							<el-input v-model="dataForm.userCard" placeholder="身份证号"></el-input>
						</el-form-item>
					</div>
					<div class="divline">
						<el-form-item prop="marrStatus" class="divlineleft" label="婚姻状况">
							<el-select v-model="dataForm.marrStatus" style="width: 100%">
									<el-option
									v-for="item in dataForm.marrlist"
									:key="item.value"
									:label="item.label"
									:value="item.value"
									/>
								</el-select>
						</el-form-item>
						<el-form-item prop="userAddr" class="divlineleft" label="籍贯">
							<el-input v-model="dataForm.userAddr" placeholder="籍贯"></el-input>
						</el-form-item>
					</div>
					<div class="divline">
						<el-form-item prop="userEdu" class="divlineleft" label="第一学历">
							<el-select v-model="dataForm.userEdu" style="width: 100%">
									<el-option
									v-for="item in dataForm.gradelist"
									:key="item.value"
									:label="item.label"
									:value="item.value"
									/>
								</el-select>
						</el-form-item>
						<el-form-item prop="levelEdu" class="divlineleft" label="最高学历">
							<el-select v-model="dataForm.levelEdu" style="width: 100%">
									<el-option
									v-for="item in dataForm.gradelist"
									:key="item.value"
									:label="item.label"
									:value="item.value"
									/>
								</el-select>
						</el-form-item>
					</div>
					<el-form-item prop="userImg" label="头像">
						<el-upload
							:action="dataForm.uploadUrl"
							:on-success="handleUpAvatar"
							:on-remove="handleRemove"
							:limit="1"
							:file-list="dataForm.fileList"
							list-type="picture-card"
							accept=".jpg,.jpeg,.png,.bmp"
						>
							<template #default>
							<el-icon><Plus /></el-icon>
							</template>
						</el-upload>
					</el-form-item>
				</el-form>
			</el-tab-pane>
		</el-tabs>
		<template #footer>
			<div class="divbtn">
				<el-button @click="visible = false">取消</el-button>
				<el-button  v-auth="'sys:user:save'" type="primary" @click="submitHandle()">确定</el-button>
			</div>
		</template>
		<!-- 弹窗, 新增 / 修改 -->
		<datalistUser ref="datalistUserRef" @fatherMethod="fatherMethod"></datalistUser>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useOrgListApi } from '@/api/sys/orgs'
import { useUserApi, useUserSubmitApi } from '@/api/sys/user'
import { usePostListApi } from '@/api/sys/post'
import { useRoleListApi } from '@/api/sys/role'
import WangEditor from '@/components/wang-editor/index.vue'
import datalistUser from './datalist.vue'
import {useAttachmentListApi } from '@/api/sys/attachment'
const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const postList = ref<any[]>([])
const roleList = ref<any[]>([])
const orgList = ref([])
const dataFormRef = ref()
const activeName = ref('first')
const dataForm = reactive({
	uploadUrl: "http://zbzx.wicp.net:8070/sys/file/upload",
	id: '',
	currentUserId:'',
	username: '',
	realName: '',
	orgId: '',
	org:'',
	orgName: '',
	password: '',
	gender: 0,
	email: '',
	mobile: '',
	roleIdList: [] as any[],
	postIdList: [] as any[],
	status: 1,
	userImg:'',
	deptId:'',
	deptName:'',
	remark:'',
	fileList:[],
	userSex:'',
	userCard:'',
	marrStatus:'',
	userAddr:'',
	userEdu:'',
	levelEdu:'',
	marrlist:[
		{ 
			value: '未婚',
    		label: '未婚',
		},
		{ 
			value: '已婚',
    		label: '已婚',
		},{ 
			value: '离异',
    		label: '离异',
		}
	],
	sexlist:[
		{ 
			value: '男',
    		label: '男',
		},
		{ 
			value: '女',
    		label: '女',
		}
	],
	gradelist:[
		{ 
			value: '高中',
    		label: '高中',
		},
		{ 
			value: '专科',
    		label: '专科',
		},
		{ 
			value: '本科',
    		label: '本科',
		},
		{ 
			value: '研究生',
    		label: '研究生',
		},
		{ 
			value: '博士生',
    		label: '博士生',
		}
	],
})
const handleUpAvatar = (res:any) => {
	console.log(res);
	if(res.code==0){
		dataForm.userImg=res.data.url;
	}
};
//删除已上传图片
const handleRemove = (file:any, fileList:any) => {
	dataForm.userImg="";
	dataForm.fileList = [];
};
const init = (id?: number,uid?:any) => {
	visible.value = true
	dataForm.id = ''
	dataForm.currentUserId=uid;//用户uid
	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}
	let orgid:any=localStorage.getItem('orgId');
	dataForm.orgId=orgid;

	let org:any=localStorage.getItem('org');
	dataForm.org=org;
	// id 存在则为修改
	if (id) {
		getUser(id)
	}
	getPostList()
	getRoleList();
	if (uid) {
	}
}
// 获取岗位列表
const getPostList = () => {
	var param={orgId:localStorage.getItem('orgId'),page:1,limit:1}
	return usePostListApi(param).then(res => {
		postList.value = res.data
	})
}

// 获取角色列表
const getRoleList = () => {
	var param={orgId:localStorage.getItem('orgId'),page:1,limit:1}
	return useRoleListApi(param).then(res => {
		roleList.value = res.data
	})
}


const datalistUserRef = ref()
const addOrUpdateDeptHandle = () => {
	datalistUserRef.value.init();
}
const fatherMethod=(orgTit:any,org:any)=>{
	dataForm.deptId=org;
	dataForm.deptName=orgTit;
}
// 获取信息
const getUser = (id: number) => {
	useUserApi(id).then(res => {
		let records:any = [{url: res.data.userImg}];
		dataForm.fileList=records;
		dataForm.remark=res.data.remark;
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({
	username: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	realName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	mobile: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	// orgId: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}
		useUserSubmitApi(dataForm).then(() => {
			
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>


<style lang="scss" scoped>
.divline {
	width: 96%;
	height: 30px;
	float: left;
	margin-top: 20px;
}
.divlineleft{
	width: 47%;
	float: left;
	height: 30px;
	padding-left: 10px;
}
.divbtn{
	text-align: center;
	margin-top: 20px;

}
.eduline{
	width: 96%;
	float: left;
	height: 50px;
	margin-top: 15px;
	border-bottom: 1px solid #BEBEBE;
	border-top: 1px solid #BEBEBE;
}
.divsplit{
	width: 96%;
	float: left;
	height: 1px;
	background-color:#409eff;
    margin-top: 5px;
}
.divsplit:last-child{
	width: 96%;
	float: left;
	height: 1px;
}
.eduitem{
	width: 20%;
	height: 30px;
	float: left;
    padding-top: 10px;
}
.inputVal.el-input__wrapper{
	width: 60%;
	float: left;
	height: 30px;
}
.inputVal.el-input{
	width: 60%;
	float: left;
	height: 30px;
}
.texttit{
	width: 30%;
	float: left;
	height: 30px;
	line-height: 2;
}
.eduitembtn{
	
	width: 19%;
	height: 30px;
	float: left;
    padding-top: 10px;
}
.filelist{
	width: 90%;
	float: left;
	margin-top: 10px;
}
.fileline{
	width: 100%;
	float: left;
	height: 30px;
}
</style>
